package element.synt;

import ast.AstItem;
import element.base.BaseNode;
import element.base.Synt;

public class Program extends Synt {
    @Override
    public AstItem toAst() {
        ast.Program program = new ast.Program();

        for (BaseNode node: this.getNodes()) {
            if (node.isSynt(Global.class)) {
                Global global = (Global) node;
                for (BaseNode node0: global.getNodes()) {
                    BaseNode node1 = ((GlobalFactor) node0).getNodes().getFirst();
                    if (node1.isSynt(Decl.class)) {
                        for (AstItem item: ((Decl) node1).toAsts()) {
                            program.globalVarDecl.add(item);
                        }
                    } else if (node1.isSynt(FuncDef.class)) {
                        program.functionsDefinition.add(((FuncDef) node1).toAst(false));
                    } else if (node1.isSynt(ClassDef.class)) {
                        program.classesDefinition.add(((ClassDef) node1).toAst());
                    }

                }
            } else if (node.isSynt(MainFuncDef.class)) {
                program.mainFunction = ((MainFuncDef) node).toAst();
            }
        }

        return program;
    }
}
